<template>
  <el-form label-width="100px" label-position="left">
    <el-form-item label="图层名称" prop="layerName">
      <el-input
        v-model.trim="formData.layerName"
        size="default"
        clearable
        style="width: 100%"
        @change="changed($event, 'layerName')"
      />
    </el-form-item>
    <el-form-item label="文本内容" prop="text">
      <el-input
        v-model.trim="formData.text"
        type="textarea"
        size="small"
        :rows="2"
        placeholder="请输入文本内容"
        @change="changed($event, 'text')"
      />
    </el-form-item>
    <el-form-item label="字体字号" prop="fontSize">
      <el-input-number
        v-model.trim="formData.fontSize"
        size="default"
        style="width: 100%"
        controls-position="right"
        @change="changed($event, 'fontSize')"
      />
    </el-form-item>
    <el-form-item label="字体颜色" prop="color">
      <ColorPicker v-model.trim="formData.color" @change="(val) => changed(val, 'color')" />
    </el-form-item>
    <el-form-item label="字体间距" prop="letterSpacing">
      <el-input-number
        v-model.trim="formData.letterSpacing"
        size="default"
        style="width: 100%"
        controls-position="right"
        @change="changed($event, 'letterSpacing')"
      />
    </el-form-item>
    <el-form-item label="字体背景" prop=" backgroundColor">
      <ColorPicker
        v-model.trim="formData.backgroundColor"
        @change="(val) => changed(val, ' backgroundColor')"
      />
    </el-form-item>
    <el-form-item label="文字粗细" prop="fontWeight">
      <el-select
        v-model="formData.fontWeight"
        clearable
        placeholder="请选择"
        @change="(val) => changed(val, 'fontWeight')"
      >
        <el-option
          v-for="item in fontWeightOptions"
          :key="item.code"
          :label="item.name"
          :value="item.code"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="对齐方式" prop="textAlign">
      <el-select
        v-model="formData.textAlign"
        clearable
        placeholder="请选择"
        @change="(val) => changed(val, 'textAlign')"
      >
        <el-option
          v-for="item in textAlignOptions"
          :key="item.code"
          :label="item.name"
          :value="item.code"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="跳转方式" prop="jumpMode">
      <el-radio-group v-model="formData.jumpMode" @change="(val) => changed(val, 'jumpMode')">
        <el-radio v-for="item in jumpModeOptions" :key="item.code" :value="item.code">{{
          item.name
        }}</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="超链地址" prop="jumpUrl">
      <el-input
        v-model.trim="formData.jumpUrl"
        size="default"
        clearable
        style="width: 100%"
        @change="changed($event, 'jumpUrl')"
      />
    </el-form-item>
  </el-form>
</template>
<script lang="ts" setup>
import { reactive, watch } from 'vue'
import ColorPicker from '../../../components/colorPicker.vue'

// 接收父组件传入的值
const props = defineProps<{
  modelValue: any
}>()

const emit = defineEmits(['update:modelValue', 'onChanged'])

const formData = reactive<any>({})

const fontWeightOptions = ref([
  { code: 'normal', name: '正常' },
  { code: 'bold', name: '粗体' },
  { code: 'bolder', name: '特粗体' },
  { code: 'lighter', name: '细体' }
])

const textAlignOptions = ref([
  { code: 'center', name: '居中' },
  { code: 'left', name: '左对齐' },
  { code: 'right', name: '右对齐' }
])

const jumpModeOptions = ref([
  {
    code: 'self',
    name: '本窗口'
  },
  {
    code: 'other',
    name: '新窗口'
  }
])

watch(
  () => props.modelValue,
  (newVal) => {
    Object.assign(formData, newVal)
  },
  { immediate: true, deep: true }
)

const changed = (val: any, key: string) => {
  emit('update:modelValue', { ...formData })
  emit('onChanged', { key, val })
}
</script>
<style lang="scss" scoped></style>
